﻿unit CmEnterpriseEmpService;

interface
{$I ../Config.inc}
uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SK0001Comm,
  CmEnterpriseEmpModel,
  CmEnterpriseEmpBLL,
  CmEmpModel,
  CmEmpService;
  
type


  TCmEnterpriseEmpService = class(TCmEnterpriseEmpBLL)
  private

  protected

  public
  {$IFDEF SERVER}
    function BeforeTreatment(const iItem:TCmEnterpriseEmp; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeTreatment(const iItems:TCmEnterpriseEmpList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeCheck(const iItem:TCmEnterpriseEmp; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeCheck(const iItems:TCmEnterpriseEmpList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function Check(const iItem:TCmEnterpriseEmp; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function Check(const iItems:TCmEnterpriseEmpList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeExecute(const iItem:TCmEnterpriseEmp; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeExecute(const iItems:TCmEnterpriseEmpList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function AfterTreatment(const iItem:TCmEnterpriseEmp; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function AfterTreatment(const iItems:TCmEnterpriseEmpList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
  {$ENDIF}
    function Save(const iLoginUserId,iOrgId,iEnterpriseId:string; const iEmpType:Integer; const iNewItems:TCmEnterpriseEmpList; out oError:string):Boolean;
  end;
  
implementation

{$IFDEF SERVER}
function TCmEnterpriseEmpService.BeforeTreatment(const iItem:TCmEnterpriseEmp; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmEnterpriseEmpService.BeforeTreatment(const iItems:TCmEnterpriseEmpList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmEnterpriseEmpService.BeforeCheck(const iItem:TCmEnterpriseEmp; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmEnterpriseEmpService.BeforeCheck(const iItems:TCmEnterpriseEmpList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmEnterpriseEmpService.Check(const iItem:TCmEnterpriseEmp; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmEnterpriseEmpService.Check(const iItems:TCmEnterpriseEmpList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmEnterpriseEmpService.BeforeExecute(const iItem:TCmEnterpriseEmp; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmEnterpriseEmpService.BeforeExecute(const iItems:TCmEnterpriseEmpList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmEnterpriseEmpService.AfterTreatment(const iItem:TCmEnterpriseEmp; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmEnterpriseEmpService.AfterTreatment(const iItems:TCmEnterpriseEmpList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;
{$ENDIF}

function TCmEnterpriseEmpService.Save(const iLoginUserId,iOrgId,iEnterpriseId:string; const iEmpType:Integer; const iNewItems:TCmEnterpriseEmpList; out oError:string):Boolean;
var
  Condition,Key:string;
  DbItems:TCmEnterpriseEmpList;
  EmpService:TCmEmpService;
  EmpItem:TCmEmp;
  SQLs:TStrings;
begin
  if (not SameStr(Trim(iLoginUserId),EmptyStr)) and
    (not SameStr(Trim(iOrgId),EmptyStr)) and
    (not SameStr(Trim(iEnterpriseId),EmptyStr)) and
    (iNewItems<>nil) then
  begin
    Condition:=Format(string('%s=''%s'' and %s=''%s'' and %s=%d'),
            [TCmEnterpriseEmpModel.cEnterpriseIdKey,iEnterpriseId,
            TCmEnterpriseEmpModel.cOrgIdKey,iOrgId,
            TCmEnterpriseEmpModel.cEmpTypeKey,iEmpType]);
    DbItems:=TCmEnterpriseEmpList.Create;
  {$IFDEF SERVER}
    EmpService:=TCmEmpService.Create(fServConn,fDBConn,fDBID);
  {$ENDIF}
  {$IFDEF CLIENT}
    EmpService:=TCmEmpService.Create(fServConn);
  {$ENDIF}
    EmpItem:=TCmEmp.Create;
    SQLs:=TStringList.Create;
    try
      if LoadByCondition(DbItems,Condition,oError) then
      begin
        for Key in iNewItems.SortKeys do
        begin
          if DbItems.IsExist(Key) then
          begin
            if not GetEditSQLs(iNewItems.Items[Key],SQLs,oError) then
            begin
              Result:=False;
              oError:=Format(string('调用TCmEnterpriseEmpService.Save方法失败, 错误内容:%s'),[oError]);
              Exit;
            end;
          end else begin
            if not GetAddSQLs(iNewItems.Items[Key],SQLs,oError) then
            begin
              Result:=False;
              oError:=Format(string('调用TCmEnterpriseEmpService.Save方法失败, 错误内容:%s'),[oError]);
              Exit;
            end;
            EmpItem.ResetObject;
            Condition:=Format(string('%s=''%s'' and %s=''%s'' and %s=%d'),
                              [TCmEmpModel.cEmpIdKey,iNewItems.Items[Key].EmpId,
                                TCmEmpModel.cOrgIdKey,iNewItems.Items[Key].OrgId,
                                TCmEmpModel.cEmpTypeKey,iNewItems.Items[Key].EmpType]);
            if EmpService.LoadByCondition(EmpItem,Condition,oError) then
            begin
              if SameStr(Trim(EmpItem.EmpId),EmptyStr) then
              begin
                EmpItem.EmpId:=iNewItems.Items[Key].EmpId;
                EmpItem.EmpType:=iNewItems.Items[Key].EmpType;
                EmpItem.OrgId:=iNewItems.Items[Key].OrgId;
                EmpItem.EmpRemark:='跟随客户-业务员映射表创建';
                EmpItem.RecordPsn:=iLoginUserId;
                EmpItem.RecordDate:=Now;
                EmpItem.TimeStamp:=TSK0001Comm.GetTimeStampMs;
                if not EmpService.GetAddSQLs(EmpItem,SQLs,oError) then
                begin
                  Result:=False;
                  oError:=Format(string('调用TCmEnterpriseEmpService.Save方法失败, 错误内容:%s'),[oError]);
                  Exit;
                end;
              end else begin
                //说明该业务员不是第一次引用，无需做任何操作
              end;
            end else begin
              Result:=False;
              oError:=Format(string('调用TCmEnterpriseEmpService.Save方法失败, 错误内容:%s'),[oError]);
              Exit;
            end;
          end;
        end;

        for Key in DbItems.SortKeys do
        begin
          if not iNewItems.IsExist(Key) then
          begin
            if not GetDeleteSQLs(DbItems.Items[Key],SQLs,oError) then
            begin
              Result:=False;
              oError:=Format(string('调用TCmEnterpriseEmpService.Save方法失败, 错误内容:%s'),[oError]);
              Exit;
            end;
          end;
        end;
      {$IFDEF SERVER}
        if not TSK0001Comm.ExecuteSQLs(fDBConn,fDBID,SQLs,oError) then
        begin
          Result:=False;
          oError:=Format(string('调用TCmEnterpriseEmpService.Save方法失败, 错误内容:%s'),[oError]);
          Exit;
        end;
      {$ENDIF}
      end else begin
        Result:=False;
        Exit;
      end;
    finally
      DbItems.Free;
      EmpService.Free;
      EmpItem.Free;
      SQLs.Free;
    end;
  end;
  Result:=True;
end;
  
end.
